class LinkedMap {
  constructor() {
    this.map = new Map();
    this.linklist = [];
  }

  set(key, value) {
    const wrappedValue = {
      value,
      key,
    };
    const old = this.map.get(key);
    this.map.set(key, wrappedValue);
    wrappedValue.index = old ? old.index:this.linklist.push(wrappedValue) - 1;
    return this;
  }

  delete(key) {
    const value = this.map.get(key);
    if (value === undefined) {
      return false;
    }
    this.map.delete(key);
    this.linklist.splice(value.index, 1);
    return true;
  }

  has(key) {
    return this.map.has(key);
  }

  toArray() {
    let {linklist} = this;
    if (arguments.length !== 0) {
      const value = this.map.get(arguments[0]);
      if (value !== undefined) {
        linklist = linklist.slice(value.index);
      }
    }
    return linklist.map(item => item.value);
  }
}

module.exports = LinkedMap;
